﻿using NPOI.SS.Formula.Functions;

using PLCS.App.Shared.Models;
using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.UserApp.Dtos;
using PLCS.Client.IService.LogManager;
using PLCS.Client.Service.Client;
using PLCS.Shared.Common.Constant;

using RestSharp;

using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace PLCS.App.ViewModels.Dialogs
{
    public class ChangePasswordViewModel : HostDialogViewModel
    {
        private HttpRestClient restClient = new HttpRestClient();
        public string Title => "修改密码";

        public event Action<IDialogResult> RequestClose;

        public ChangePasswordViewModel(IOperationLogService logShowService)
        {
            View = "修改密码界面";
            this._logShowService = logShowService;
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        private UserChangePassWordDto _userChangePasswordModel = new UserChangePassWordDto();
        private readonly IOperationLogService _logShowService;

        public UserChangePassWordDto userChangePassWordModel
        {
            get { return _userChangePasswordModel; }
            set { SetProperty(ref _userChangePasswordModel, value); }
        }

        public DelegateCommand CloseCommand => new DelegateCommand(() => RequestClose?.Invoke(new DialogResult(ButtonResult.OK)));

        public DelegateCommand ChangePasswordCommand => new DelegateCommand(ChangePassword);

        private async void ChangePassword()
        {
            userChangePassWordModel.UserName = GlobalVariable.CurrentUser.UserName;

            RequestParam requestParam = new RequestParam(Method.Post, "user/changePassword", userChangePassWordModel);
            var res = await restClient.ExecuteAsync(requestParam);
            if (res.Status)
            {
                Log("修改密码", new { Object = GlobalVariable.CurrentUser.UserName });
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                //Msg.Info("修改密码成功");
            }
            else
            {
                //Msg.Error(res.Message);
            }
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
        }
    }
}